package com.bw.dao.impl;

import com.bw.dao.GoodsDao;
import com.bw.pojo.Goods;
import com.bw.pojo.vo.GoodsVo;

import java.util.List;

public class GoodsDaoImpl extends BaseDao implements GoodsDao {
    //列表
    @Override
    public List<GoodsVo> getGoodsList(GoodsVo goodsVo) {
        String sql = "select t_goods.*,t_supplier.supplier_name,t_goods_type.goods_type_name from t_goods left join " +
                " t_supplier using(supplier_id) left join t_goods_type using(goods_type_id) where t_goods.deleted = 0";
        //搜索框where 条件,分页同时存在 请先写谁 sql: where > 分组 > 排序 > 分页
        if(null != goodsVo.getGoodsName() && !goodsVo.getGoodsName().isEmpty()){
            sql+=" and INSTR(goods_name,'"+goodsVo.getGoodsName()+"')";
        }
        //分页 limit 起始下标, 条数
        sql += " limit ?,?";
        //起始下标 = （当前页数 -1） * 条数
        int index = (goodsVo.getPageNum()-1)*goodsVo.getPageSize();
        //执行sql
        List<GoodsVo> list = baseQueryList(GoodsVo.class, sql, index, goodsVo.getPageSize());
        return list;
    }
    //总条数
    @Override
    public int getCount(GoodsVo goodsVo) {
        String sql = "select count(1) from t_goods where deleted = 0 ";
        //搜索框where 条件,分页同时存在 请先写谁 sql: where > 分组 > 排序 > 分页
        if(null != goodsVo.getGoodsName() && !goodsVo.getGoodsName().isEmpty()){
            sql+=" and INSTR(goods_name,'"+goodsVo.getGoodsName()+"')";
        }
        Long aLong = baseQueryCount(Long.class, sql);
        return aLong.intValue();
    }
    //删除
    @Override
    public void getToDel(String[] goodsUid) {
        String sql = "update t_goods set deleted = 1 where goods_id in ("+goodsUid+")";
        baseUpdate(sql);
    }
    //回显change
    @Override
    public GoodsVo findById(Integer goodsId) {
        String sql = "select * from t_goods where goods_id = '"+goodsId+"'";
        GoodsVo goodsVo = baseQueryOne(GoodsVo.class, sql);
        return goodsVo;
    }
    //添加
    @Override
    public void getGoodsAdd(Goods goods) {
        String sql = "insert into t_goods(goods_code,goods_name,inventory_quantity,goods_type_id,supplier_id,deleted) values (?,?,?,?,?,0)";
        baseUpdate(sql,goods.getGoodsCode(),goods.getGoodsName(),goods.getInventoryQuantity(),goods.getGoodsTypeId(),goods.getSupplierId());
    }

    @Override
    public void getGoodsUpd(Goods goods) {
        String sql = "update t_goods set goods_code=?,goods_name=?,inventory_quantity=?,goods_type_id=?,supplier_id=?" +
                " where goods_id=?";
        baseUpdate(sql,
                goods.getGoodsCode(),
                goods.getGoodsName(),
                goods.getInventoryQuantity(),
                goods.getGoodsTypeId(),
                goods.getSupplierId(),
                goods.getGoodsId()
                );
    }

    @Override
    public void getAdd(Goods goods) {
        String sql = "insert into t_goods(goods_code,goods_name,inventory_quantity,goods_type_id,supplier_id,deleted) values (?,?,?,?,?,0)";
        baseUpdate(sql,goods.getGoodsCode(),goods.getGoodsName(),goods.getInventoryQuantity(),goods.getGoodsTypeId(),goods.getSupplierId());
    }

    @Override
    public void getUpd(Goods goods) {
        String sql = "update t_goods set goods_code=?,goods_name=?,inventory_quantity=?,goods_type_id=?,supplier_id=?" +
                " where goods_id=?";
        baseUpdate(sql,
                goods.getGoodsCode(),
                goods.getGoodsName(),
                goods.getInventoryQuantity(),
                goods.getGoodsTypeId(),
                goods.getSupplierId(),
                goods.getGoodsId()
        );
    }

    @Override
    public List<Goods> findGoodsBySupplierId(Integer supplierId) {
        String sql = "select * from t_goods where supplier_id = ? ";
        return baseQueryList(Goods.class, sql, supplierId);
    }

    @Override
    public List<Goods> getGoodsSupplierId(Integer supplierId) {
        String sql = "select * from t_goods where supplier_id = ? ";
        return baseQueryList(Goods.class, sql, supplierId);
    }
}
